// pages/Menbercenter/MenberAddr/MemberUpdateAddr/MemberUpdateAddr.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_defaultaddr: '0',
    isSelectCity: false,
    province:'',
    region: ["", "", ""]
  },
  // 选择地区
  changeRegin(e) {
    this.setData({
      region: e.detail.value,
      isSelectCity: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var ua_id = options.id
    wx.request({
      url: app.data.hostUrl + '/api/weChat.do?selectOneAddr',//自己的服务接口地址
      data: {
        id: ua_id
      },
      success: function (data) {
        console.log(data.data) 
        that.setData({
          addr: data.data,
          id:data.data.id
        })
      },
      fail: function () {
        console.log('查询失败')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 编辑地址
  // coustomerId receivePerson  mobile  receiveAddress  isDefault
  formSubmit: function (e) {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var did = this.data.id;
    // 获取姓名
    var realName = e.detail.value.contactsuser;
    // 获取手机号码
    var mobile = e.detail.value.mobile;

    // 获取详细地址
    var address = e.detail.value.addr;
    // 验证手机号
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;

    // 判断姓名是否为空
    if (realName.length == 0) {
      wx.showModal({
        title: '提示',
        content: '姓名不能为空',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (mobile.length == 0) {
      // 判断手机号是否为空
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!myreg.test(mobile)) {
      // 判断身份证号的格式
      wx.showModal({
        title: '提示',
        content: '手机号输入有误',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.isSelectCity == false) {
      wx.showModal({
        title: '提示',
        content: '请选择地址',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (address.length == 0) {
      wx.showModal({
        title: '提示',
        content: '地区不能为空',
        showCancel: false,
        success: function (res) {
        }
      })
    } else {
      wx.request({
        url: app.data.hostUrl + '/api/weChat.do?updateAddr ',//自己的服务接口地址
        data: {
          customerId: user_id,
          id:did,
          receivePerson: realName,
          mobile: mobile,
          region: that.data.region[0] + that.data.region[1] + that.data.region[2],
          receiveAddress: address,
          isDefault: that.data.is_user
        },
        method: "POST",
        header: "ContentType:application/json",
        success: function (data) {
          console.log(data)
          // 成功
          if (data.data.status == 1) {
            wx.showModal({
              title: '提示',
              content: '修改成功',
              showCancel: false,
              success: function (res) {
                // 返回上一个页面的方法
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          } else {
            // 失败
            wx.showModal({
              title: '提示',
              content: '修改失败',
              showCancel: false,
              success: function (res) {
              }
            })
          }
        },
        fail: function () {
          console.log('添加失败')
        }
      })
    }
  }
})